﻿using System;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Generic;

using VIT.DataHelper.DataAccess;

namespace VIT.Wedding.Modules.ColorNStyleScroll
{
    public partial class ColorNStyleScroll : VIT.Web.UI.VITModule
    {
        private int Appropriate
        {
            get { return Convert.ToInt32(Params["Appropriate"]); }
        }

        protected string ColorNStyleModuleName
        {
            get { return Params["ColorNStyleModuleName"]; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (ColorNStyleModuleName == "Colors")
            {
                if (Appropriate > 0)
                {
                    VITData.OpenTransaction();
                    var colors = VITData.GetQuery<Product_Color>();
                    var styles = VITData.GetQuery<Product_Color_Style>();
                    var result = (from color in colors
                                  join style in styles on color.Id equals style.ColorId
                                  where style.StyleId == Appropriate
                                  select color).Distinct().ToArray();
                    VITData.RollbackTransaction();
                    rpt.DataSource = result;
                }
                else
                {
                    var result = VITData.GetQuery<Product_Color>();
                    rpt.DataSource = result;
                }
            }
            else if (ColorNStyleModuleName == "Styles")
            {
                if (Appropriate > 0)
                {
                    VITData.OpenTransaction();
                    var types = VITData.GetQuery<Product_Style>();
                    var styles = VITData.GetQuery<Product_Color_Style>();
                    var result = (from type in types
                                  join style in styles on type.Id equals style.StyleId
                                  where style.ColorId == Appropriate
                                  select type).Distinct().ToArray();
                    VITData.RollbackTransaction();
                    rpt.DataSource = result;
                }
                else
                {
                    var result = VITData.GetQuery<Product_Style>();
                    rpt.DataSource = result;
                }
            }
            rpt.DataBind();
        }
    }
}